# -*- coding:utf-8 -*-
# @Time : 21-8-24 上午11:33
# @Author : zdy
# @File : UIDAQ.py
# desc: 采集卡调试窗口：
#       采集卡未连接时，提供命令调试功能，可修改串口，重新
#       采集卡连接时，提供命令调试功能，重新连接功能？

from Class.Devices import *
from UI.DAQ import Ui_DAQ
import os
from importQT import *

class UIDAQ(QDialog, Ui_DAQ):
    def __init__(self):
        super(UIDAQ, self).__init__()
        self.setupUi(self)
        self.setWindowFlags(Qt.WindowCloseButtonHint)
        self.setFixedSize(self.width(), self.height())
        self.setAttribute(Qt.WA_DeleteOnClose)
        self.applyResource()
        # 加载可用串口
        # port_list = list(serial.tools.list_ports.comports())
        # for port in port_list:
        #     if port[0].__contains__('USB'):
        #         self.port_cbb.addItem(port[0])
        self.port_cbb.addItem(device.getDAQCom())
        _translate = QCoreApplication.translate
        self.parity_cbb.setItemText(0, _translate("Form", "N"))
        self.open_btn.clicked.connect(self.port_open)
        self.send_btn.clicked.connect(self.data_send)
        self.btnStopRefresh.clicked.connect(self.btnStopRefresh_clicked)
        device.card.sigReceiveHex.connect(self.refresh)
        self.boud_cbb.setEnabled(False)
        self.data_cbb.setEnabled(False)
        self.stop_cbb.setEnabled(False)
        self.parity_cbb.setEnabled(False)
        device.card.sigInfo.connect(self.refreshInfo)
        self.setStatue()
        self.isrefresh = True

    def applyResource(self):
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap(":/pic/icon.ico"), QtGui.QIcon.Normal, QtGui.QIcon.On)
        self.setWindowIcon(icon)

    def setStatue(self):
        '''根据连接状态，设置控件状态'''
        if device.card.isLinked:
            self.open_btn.setEnabled(False)
            self.port_cbb.setEnabled(False)
            self.refreshInfo("采集卡已连接")
        else:
            self.open_btn.setEnabled(True)
            self.port_cbb.setEnabled(True)
            self.refreshInfo("采集卡未连接")

    def refreshInfo(self, str):
        '''刷新提示信息'''
        self.txtStatue.append(str)

    def refresh(self, s):
        '''刷新接收数据'''
        if self.isrefresh:
            self.receive_text.insertPlainText(s + os.linesep)
            # 获取到text光标
            textCursor = self.receive_text.textCursor()
            # 滚动到底部
            textCursor.movePosition(textCursor.End)
            # 设置光标到text中去
            self.receive_text.setTextCursor(textCursor)

    def port_open(self):
        '''连接：未连接状态下可用；若当前串口已打开，则先关闭，再连接；'''
        device.close_card()
        device.setDAQCom(self.port_cbb.currentText())
        device.link_card()
        self.setStatue()

    def btnStopRefresh_clicked(self):
        self.isrefresh = bool(1- self.isrefresh)
        if self.isrefresh:
            self.btnStopRefresh.setText("停止刷新")
        else:
            self.btnStopRefresh.setText("继续刷新")

    # 发送数据
    def data_send(self):
        if device.card.isOpen():
            input_s = self.send_text.toPlainText()
            if input_s != "":
                # hex发送
                input_s = input_s.strip()
                send_list = []
                while input_s != '':
                    try:
                        num = int(input_s[0:2], 16)
                    except ValueError:
                        QMessageBox.critical(self, 'wrong data', '请输入十六进制数据，以空格分开!')
                        return None
                    input_s = input_s[2:].strip()
                    send_list.append(num)
                input_s = bytes(send_list)
                device.card.data_send(input_s)
        else:
            QMessageBox.critical(self, "提示", "串口未打开")
